﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Collections.Generic;
using System.Text;


namespace AR
{
	public  interface INavigationService : INavigate
	{
        void Navigate(string uri, IDictionary<string, string> parameters);
		//static void Navigate(string url);
	}

	public class NavigationService : INavigationService
	{
        private static Frame _frame= (((BusyIndicator)(Application.Current.RootVisual)).Content as MainPage).ContentFrame;

		#region Events
		public event NavigatedEventHandler Navigated;
		public event NavigatingCancelEventHandler Navigating;
		public event NavigationFailedEventHandler NavigationFailed;
		public event NavigationStoppedEventHandler NavigationStopped;
		#endregion

		public static NavigationService Instance { get; private set; }

		static NavigationService()
		{
			Instance = new NavigationService();
		}

		private NavigationService()
		{
			  
		}

		public static Frame Frame
		{
			get
			{
				if (_frame == null)
					throw new Exception("Frame is not initialized");
				return _frame;
			}
		}

		public void InitializeFrame(Frame frame)
		{
			_frame = frame;

			_frame.Navigated += (sender, e) =>
				{
					if (Navigated != null)
						Navigated(this, e);
				};
			_frame.Navigating += (sender, e) =>
			{
				if (Navigating != null)
					Navigating(this, e);
			};
			_frame.NavigationFailed += (sender, e) =>
				{
				if (NavigationFailed != null)
					NavigationFailed(this, e);
				};

			_frame.NavigationStopped += (sender, e) =>
			{
				if (NavigationStopped != null)
					NavigationStopped(this, e);
			};
		}

	  
		public string GetQueryStringParameterValue(string parameterName)
		{
			string[] paramsArray = Frame.CurrentSource.ToString().Split('/');
			foreach (var item in paramsArray)
			{
				if (!item.Contains("="))
					continue;
				string[] parameter = item.Split('=');
				if (parameter[0] == parameterName)
					return parameter[1];
			}
			return string.Empty;
		}


		#region INavigationService Members

		public static void Navigate(string url)
		{
			Frame.Navigate(new Uri(url, UriKind.RelativeOrAbsolute));
		}

		#endregion

		#region INavigate Members

		public bool Navigate(Uri source)
		{
		   return Frame.Navigate(source);
		}
        public static void Navigate2(string uri, IDictionary<string, string> parameters)
        {
            Frame frame2 = ((MainPage)(Application.Current.RootVisual)).ContentFrame;
            if (frame2 == null)
            {
                return;
            }

            StringBuilder uriBuilder = new StringBuilder();
            uriBuilder.Append(uri);
            if (parameters != null && parameters.Count > 0)
            {
                uriBuilder.Append("?");
                bool prependAmp = false;
                foreach (KeyValuePair<string, string> parameterPair in parameters)
                {
                    if (prependAmp)
                    {
                        uriBuilder.Append("&");
                    }
                    uriBuilder.AppendFormat("{0}={1}", parameterPair.Key, parameterPair.Value);
                    prependAmp = true;
                }
            }

            uri = uriBuilder.ToString();
            frame2.Navigate(new Uri(uri, UriKind.RelativeOrAbsolute));
        }

		#endregion



        public void Navigate(string uri, IDictionary<string, string> parameters)
        {
            throw new NotImplementedException();
        }


      
    }
}
